//
//  VerificationViewController.swift
//  App_Login
//
//  Created by 蒋晨成 on 2022/1/4.
//

import Adaptor
import Center
import Chain
import Foundation
import RxCocoa
import RxSwift
import Service
import SnapKit
import Toast
import UICore
import UIKit

class VerifyViewController: ViewController {
    lazy var contentView: UIView = .init()
        .x
        .backgroundColor(Adaptor.Login.background)
        .instance

    lazy var titleLabel: UILabel = .init()
        .x
        .font(.boldSystemFont(ofSize: 36))
        .textColor(Adaptor.Login.Verify.titleColor)
        .text(LoginModular.localizedString(key: "Verify.Title"))
        .instance

    lazy var mobileLabel: UILabel = .init()
        .x
        .font(.systemFont(ofSize: 18))
        .textColor(Adaptor.Login.Verify.textColor)
        .text("\(LoginModular.localizedString(key: "Verify.Label.Mobile"))")
        .instance

    lazy var codeView: UIStackView = .init()
        .x
        .backgroundColor(Adaptor.Login.background)
        .axis(.horizontal)
        .spacing(24.0)
        .instance

    lazy var code1Label: UILabel = .init()
        .x
        .font(.boldSystemFont(ofSize: 34))
        .textColor(Adaptor.Login.Verify.titleColor)
        .textAlignment(.center)
        .instance

    lazy var code2Label: UILabel = .init()
        .x
        .font(.boldSystemFont(ofSize: 34))
        .textColor(Adaptor.Login.Verify.titleColor)
        .textAlignment(.center)
        .instance

    lazy var code3Label: UILabel = .init()
        .x
        .font(.boldSystemFont(ofSize: 34))
        .textColor(Adaptor.Login.Verify.titleColor)
        .textAlignment(.center)
        .instance

    lazy var code4Label: UILabel = .init()
        .x
        .font(.boldSystemFont(ofSize: 34))
        .textColor(Adaptor.Login.Verify.titleColor)
        .textAlignment(.center)
        .instance

    lazy var line1: UIView = .init()
        .x
        .backgroundColor(Adaptor.Login.Verify.normalLineColor)
        .instance

    lazy var line2: UIView = .init()
        .x
        .backgroundColor(Adaptor.Login.Verify.normalLineColor)
        .instance

    lazy var line3: UIView = .init()
        .x
        .backgroundColor(Adaptor.Login.Verify.normalLineColor)
        .instance

    lazy var line4: UIView = .init()
        .x
        .backgroundColor(Adaptor.Login.Verify.normalLineColor)
        .instance

    lazy var codeTextField: UITextField = .init()
        .x
        .textColor(.clear)
        .keyboardType(.numberPad)
        .instance

    lazy var timerButton: UIButton = .init(type: .custom)
        .x
        .addTarget(self, action: #selector(timerButtonAction), for: .touchUpInside)
        .titleFont(font: .systemFont(ofSize: 16))
        .enabled(false)
        .setTitleColor(Adaptor.Login.Verify.textColor, for: .disabled)
        .setTitle("\(60)\(LoginModular.localizedString(key: "Verify.Label.Timer"))", for: .disabled)
        .setTitleColor(Adaptor.Login.Verify.timeoutTextColor, for: .normal)
        .setTitle(LoginModular.localizedString(key: "Verify.Label.Timer.Timeout"), for: .normal)
        .instance

    lazy var disposeBag = DisposeBag()
    lazy var viewModel = VerifyViewModel()
    /**
        true: 需要发送短信
        false: 标识不发送，只倒计时
     */
    let sendSMS = PublishSubject<Bool>()
}

// MARK: - Override

extension VerifyViewController {
    override func viewDidLoad() {
        super.viewDidLoad()

        setupNav()
        setup()
        bind()
    }
}

extension VerifyViewController {
    func setupNav() {
        if (navigationController?.viewControllers.count ?? 0) > 1 {
            let backImage = LoginModular.image(named: "Common.Nav.Action.Back")
            addDefaultLeftBarButtonItem(image: backImage)
        }

        setNavigationBarTintColor(tintColor: LoginModular.color(name: "App.Dark1"))
    }

    func setup() {
        contentView.x.add(to: view).snp.makeConstraints {
            $0.edges.equalToSuperview()
        }

        titleLabel.x.add(to: contentView).snp.makeConstraints {
            $0.leading.equalTo(46)
            $0.trailing.equalTo(-46)
            $0.top.equalTo(contentView.safeAreaLayoutGuide.snp.top).offset(40)
        }

        mobileLabel.x.add(to: contentView).snp.makeConstraints {
            $0.leading.equalTo(46)
            $0.trailing.equalTo(-46)
            $0.top.equalTo(titleLabel.snp.bottom).offset(12)
        }

        codeTextField.x.add(to: contentView).snp.makeConstraints {
            $0.centerX.equalToSuperview()
            $0.height.equalTo(44)
            $0.top.equalTo(mobileLabel.snp.bottom).offset(54)
        }

        codeView.x.add(to: contentView).snp.makeConstraints {
            $0.centerX.equalToSuperview()
            $0.height.equalTo(44)
            $0.top.equalTo(mobileLabel.snp.bottom).offset(54)
        }

        codeView.addArrangedSubview(code1Label)
        codeView.addArrangedSubview(code2Label)
        codeView.addArrangedSubview(code3Label)
        codeView.addArrangedSubview(code4Label)

        code1Label.snp.makeConstraints {
            $0.width.equalTo(40)
        }
        code2Label.snp.makeConstraints {
            $0.width.equalTo(40)
        }
        code3Label.snp.makeConstraints {
            $0.width.equalTo(40)
        }
        code4Label.snp.makeConstraints {
            $0.width.equalTo(40)
        }

        line1.x.add(to: code1Label).snp.makeConstraints {
            $0.trailing.leading.bottom.equalToSuperview()
            $0.height.equalTo(4)
        }

        line2.x.add(to: code2Label).snp.makeConstraints {
            $0.trailing.leading.bottom.equalToSuperview()
            $0.height.equalTo(4)
        }

        line3.x.add(to: code3Label).snp.makeConstraints {
            $0.trailing.leading.bottom.equalToSuperview()
            $0.height.equalTo(4)
        }

        line4.x.add(to: code4Label).snp.makeConstraints {
            $0.trailing.leading.bottom.equalToSuperview()
            $0.height.equalTo(4)
        }

        timerButton.x.add(to: contentView).snp.makeConstraints {
            $0.leading.equalTo(46)
            $0.top.equalTo(codeView.snp.bottom).offset(30)
        }

        codeTextField.becomeFirstResponder()
    }
}

extension VerifyViewController: Routable {
    class func initialize(values _: [String: Any]) -> ViewController? {
        VerifyViewController(nibName: nil, bundle: nil)
    }

    static let routePattern = "login/verify"
}
